package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.data.ResGoodsCreateData;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.AdminServiceImpl;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.GoodsServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.StringUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Yuye Wang
 * @date 2023/5/31 10:01
 * @description TODO
 */
@WebServlet("/admin/admin/*")
public class AdminAdminServlet extends WdBaseServlet {
    private AdminService adminService = new AdminServiceImpl();

    private BaseRespVo list(HttpServletRequest request, HttpServletResponse response) {
        //获取参数
        String pageStr = request.getParameter("page");
        Integer page = StringUtil.isEmpty(pageStr) ? null : Integer.parseInt(pageStr);

        String limitStr = request.getParameter("limit");
        Integer limit = StringUtil.isEmpty(limitStr) ? null : Integer.parseInt(limitStr);

        String username = request.getParameter("username");

        String sort = request.getParameter("sort");

        String order = request.getParameter("order");

        //调用查询服务
        return adminService.list(page, limit, username, sort, order);
    }

    @SneakyThrows
    private BaseRespVo create(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketAdmin marketAdmin = JacksonUtil.read(jsonStr, MarketAdmin.class);

        return adminService.create(marketAdmin);
    }

    @SneakyThrows
    private BaseRespVo update(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketAdmin marketAdmin = JacksonUtil.read(jsonStr, MarketAdmin.class);

        return adminService.update(marketAdmin);
    }

    @SneakyThrows
    private BaseRespVo delete(HttpServletRequest request, HttpServletResponse response) {
        String jsonStr = request.getReader().readLine();
        MarketAdmin marketAdmin = JacksonUtil.read(jsonStr, MarketAdmin.class);

        return adminService.delete(marketAdmin);
    }


}
